package com.ruoyi.supplier.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.supplier.domain.TbProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.supplier.domain.TbProductSpecItem;
import com.ruoyi.supplier.mapper.TbProductSpecMapper;
import com.ruoyi.supplier.domain.TbProductSpec;
import com.ruoyi.supplier.service.ITbProductSpecService;

/**
 * 商品规格Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-02-16
 */
@Service
public class TbProductSpecServiceImpl implements ITbProductSpecService 
{
    @Autowired
    private TbProductSpecMapper tbProductSpecMapper;

    /**
     * 查询商品规格
     * 
     * @param id 商品规格ID
     * @return 商品规格
     */
    @Override
    public TbProductSpec selectTbProductSpecById(Long id)
    {
        return tbProductSpecMapper.selectTbProductSpecById(id);
    }

    /**
     * 查询商品规格列表
     * 
     * @param tbProductSpec 商品规格
     * @return 商品规格
     */
    @Override
    public List<TbProductSpec> selectTbProductSpecList(TbProductSpec tbProductSpec)
    {
        return tbProductSpecMapper.selectTbProductSpecList(tbProductSpec);
    }

    @Override
    public List<TbProductSpec> selectTbProductSpecByTypeId(Long typeId) {
        return tbProductSpecMapper.selectTbProductSpecByTypeId(typeId);
    }

    /**
     * 新增商品规格
     * 
     * @param tbProductSpec 商品规格
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbProductSpec(TbProductSpec tbProductSpec)
    {
        tbProductSpec.setCreateTime(DateUtils.getNowDate());
        tbProductSpec.setCreateBy(SecurityUtils.getUsername());
        int rows = tbProductSpecMapper.insertTbProductSpec(tbProductSpec);
        insertTbProductSpecItem(tbProductSpec);
        return rows;
    }

    /**
     * 修改商品规格
     * 
     * @param tbProductSpec 商品规格
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbProductSpec(TbProductSpec tbProductSpec)
    {
        tbProductSpec.setUpdateTime(DateUtils.getNowDate());
        tbProductSpec.setUpdateBy(SecurityUtils.getUsername());
        tbProductSpecMapper.deleteTbProductSpecItemBySpecId(tbProductSpec.getId());
        insertTbProductSpecItem(tbProductSpec);
        return tbProductSpecMapper.updateTbProductSpec(tbProductSpec);
    }

    /**
     * 批量删除商品规格
     * 
     * @param ids 需要删除的商品规格ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbProductSpecByIds(Long[] ids)
    {
        tbProductSpecMapper.deleteTbProductSpecItemBySpecIds(ids);
        return tbProductSpecMapper.deleteTbProductSpecByIds(ids);
    }

    /**
     * 删除商品规格信息
     * 
     * @param id 商品规格ID
     * @return 结果
     */
    @Override
    public int deleteTbProductSpecById(Long id)
    {
        tbProductSpecMapper.deleteTbProductSpecItemBySpecId(id);
        return tbProductSpecMapper.deleteTbProductSpecById(id);
    }

    @Override
    public int checkTbProductSpec(TbProduct tbProduct) {
        List<TbProductSpec> tbProductSpecs = tbProductSpecMapper.selectTbProductSpecByTypeId(tbProduct.getTypeId());
        if(tbProductSpecs == null ){
            return 0;
        }
        return tbProductSpecs.size();
    }

    /**
     * 新增商品规格明细信息
     * 
     * @param tbProductSpec 商品规格对象
     */
    public void insertTbProductSpecItem(TbProductSpec tbProductSpec)
    {
        List<TbProductSpecItem> tbProductSpecItemList = tbProductSpec.getTbProductSpecItemList();
        Long id = tbProductSpec.getId();
        if (StringUtils.isNotNull(tbProductSpecItemList))
        {
            List<TbProductSpecItem> list = new ArrayList<TbProductSpecItem>();
            for (TbProductSpecItem tbProductSpecItem : tbProductSpecItemList)
            {
                tbProductSpecItem.setSpecId(id);
                list.add(tbProductSpecItem);
            }
            if (list.size() > 0)
            {
                tbProductSpecMapper.batchTbProductSpecItem(list);
            }
        }
    }
}
